<?php
/** 
*
* @package phpBB3
* 
* @author David Lewis ( http://startrekguide.com ) - Highway of Life
* 
* @version $Id: index.php 72 2007-06-25 12:48:35Z highwayoflife $
* @copyright (c) 2007 Highway of Life
* @license undetermined
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'newsletter/functions_newsletter.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

$newsletter_version = '1.0.B1';

// define the table names
define('NEWSLETTER_POST_TABLE', $table_prefix . 'newsletter_post');
define('NEWSLETTER_TABLE',		$table_prefix . 'newsletter');

// Start session management
$user->session_begin();
$auth->acl($user->data);

$sql = 'SELECT style_id FROM ' . STYLES_TABLE . " WHERE style_name = 'prosilver'";
$db->sql_query($sql, 2678400); // caching it for a 31 days
$style_id = $db->sql_fetchfield('style_id');

$user->setup(array('mods/permissions_newsletter', 'mods'), $style_id);

// check to see if the newsletter is installed. If not, install it.
if (!isset($config['newsletter_version']))
{
	install_newsletter();
}
else if (version_compare($config['newsletter_version'], $newsletter_version, '<'))
{
	// the current version is older... update it.
	update_newsletter();
}

$submit			= isset($_REQUEST['submit']) ? true : false;
$start			= request_var('start', 0); // pagination
$sort_dir		= request_var('sd', 'a'); // sort direction for list
$sort_key		= request_var('sk', ''); // not used yet
$view			= strtolower(request_var('view', '')); // last, list, detail, email
$newsletter_id	= request_var('news', 0); // specific newsletter ID

$view_ary = array('email', 'list', 'last', 'detail');

if (($newsletter_id && !$view) || ($newsletter_id && !in_array($view, $view_ary)))
{
	$view = 'detail';
}
else if ((!$newsletter_id && !$view) || (!$newsletter_id && !in_array($view, $view_ary)))
{
	$view = 'list';
}

// set some inital vars...
$total_newsletters = 1;
// use the same per-page as the posts config
$sql_limit = $config['posts_per_page'];
$sql_start = $start;

if ($view != 'list')
{
	// there is no news ID, and the default view is not list, or if the view is "last"
	// then grab the last newsletter id based on date.
	$sql = 'SELECT n.*
			FROM ' . NEWSLETTER_TABLE . ' n';
	$sql .= ($newsletter_id) ? ' WHERE n.newsletter_id = ' . $newsletter_id : ' ORDER BY n.newsletter_date DESC LIMIT 0,1';
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		$newsletter_id = $row['newsletter_id'];
		
		$template->assign_vars(array(
			'NEWSLETTER_SUBJECT'	=> $row['newsletter_subject'],
			'NEWSLETTER_TITLE'		=> $row['newsletter_title'],
			'NEWSLETTER_DESCRIPTION'=> $row['newsletter_desc'],
			'NEWSLETTER_DATE'		=> $user->format_date($row['newsletter_date']),
		));
		
		$page_title = $row['newsletter_subject'];
	}
	$db->sql_freeresult($result);
}
else
{
	// count the newsletters for the list.
	$sql = 'SELECT COUNT(newsletter_id) AS num_newsletters
			FROM ' . NEWSLETTER_TABLE;
	$result = $db->sql_query($sql);
	$total_newsletters = (int) $db->sql_fetchfield('num_newsletters');
	$db->sql_freeresult($result);
	
	$page_title = $user->lang['NEWSLETTER_ARCHIVE'];
}

// setup the nav links for breadcrumbs in the header.
$template->assign_block_vars('navlinks', array(
	'FORUM_NAME'	=> $user->lang['NEWSLETTER_ARCHIVE'],
	'U_VIEW_FORUM'	=> append_sid("{$phpbb_root_path}newsletter/index.$phpEx", 'view=list'),
));

// query setup for a single newsletter
if ($newsletter_id && $view != 'list')
{
	$sql_array = array(
		'SELECT' => 'np.*,
	t.forum_id, p.post_time, p.enable_bbcode, p.enable_smilies, p.enable_magic_url, p.post_text, p.bbcode_bitfield, p.bbcode_uid,
	u.username, u.user_colour, u.user_posts, u.user_rank, u.user_avatar, u.user_avatar_width, u.user_avatar_height, u.user_avatar_type',
		'FROM' => array(
			NEWSLETTER_POST_TABLE => 'np',
		),
		'LEFT_JOIN' => array(
			array(
				'FROM'	=> array(USERS_TABLE => 'u'),
				'ON'	=> 'u.user_id = np.post_author_id',
			),
			array(
				'FROM'	=> array(POSTS_TABLE => 'p'),
				'ON'	=> 'p.post_id = np.post_id',
			),
			array(
				'FROM'	=> array(TOPICS_TABLE => 't'),
				'ON'	=> 't.topic_id = np.topic_reply_id',
			),
		),
		'WHERE'		=> 'np.newsletter_id = ' . $newsletter_id,
		'ORDER_BY'	=> 'np.post_order_id ASC, np.news_post_date DESC',
	);
}
else 
{
	// query setup for the list of newsletters
	$sql_array = array(
		'SELECT'	=> 'n.*',
		'FROM'		=> array(
			NEWSLETTER_TABLE	=> 'n',
		),
		'ORDER_BY' => 'n.newsletter_date ' . (($sort_dir == 'a') ? 'ASC' : 'DESC'),
	);
}

// build the query from the array
$sql = $db->sql_build_query('SELECT', $sql_array);

// cache the newsletter query if it is only a single query
if ($newsletter_id)
{
	$result = $db->sql_query($sql); //, 86400);
}
else
{
	// for the list, we need to sort out the pagination
	$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
}

$server_path = ($view == 'email') ? generate_board_url() . '/' : $phpbb_root_path;

while ($row = $db->sql_fetchrow($result))
{
	switch ($view)
	{
		case 'detail':
		case 'last':
		case 'email':
			get_user_rank($row['user_rank'], $row['user_posts'], $rank_title, $rank_img, $rank_img_src);
			$row['bbcode_options'] = (($row['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) + (($row['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) + (($row['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
			
			$topic_id = 't=' . $row['topic_reply_id'];
			$forum_id = ($row['forum_id']) ? '&amp;f=' . $row['forum_id'] : '&amp;f=2';
			
		
			$template->assign_block_vars('newsrow', array(
				'POST_DATE'		=> $user->format_date($row['news_post_date']),
				'POST_DESC'		=> $row['news_post_desc'],
				'POST_HEADER'	=> $row['news_post_header'],
				
				'U_TOPIC'		=> "{$server_path}viewtopic.$phpEx?t=$topic_id$forum_id",
				'U_VIEW_TOPIC'	=> append_sid("{$server_path}viewtopic.$phpEx", "$forum_id&amp;$topic_id"),
				'U_FORUM'		=> "{$server_path}viewforum.$phpEx?$forum_id",
				'U_VIEW_FORUM'	=> append_sid("{$server_path}viewforum.$phpEx", $forum_id),
				'U_TOPIC_REPLY'	=> append_sid("{$server_path}posting.$phpEx", "mode=reply$forum_id&amp;$topic_id"),
				
				'U_USER_PROFILE'=> get_username_string('profile', $row['post_author_id'], $row['username']),
				'USERNAME'		=> get_username_string('username', $row['post_author_id'], $row['username']),
				'USER_COLOUR'	=> get_username_string('colour', $row['post_author_id'], $row['username'], $row['user_colour']),
				'USERNAME_FULL'	=> get_username_string('full', $row['post_author_id'], $row['username'], $row['user_colour']),
				'USER_AVATAR'	=> get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height'], $row['username']),
				'USER_RANK'		=> $rank_title,
				'USER_RANK_IMG'	=> $rank_img,
				
				'POST_CONTENT'	=> generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']),
			));
		break;
		
		case 'list':
		default:
			$template->assign_block_vars('newslist', array(
				'NEWSLETTER_DATE'	=> $user->format_date($row['newsletter_date'], false, true),
				'NEWSLETTER_ID'		=> $row['newsletter_id'],
				'NEWSLETTER_TITLE'	=> generate_text_for_display($row['newsletter_title'], '', '', ''),
				'NEWSEETTER_SUBJECT'=> generate_text_for_display($row['newsletter_subject'], '', '', ''),
				'NEWSLETTER_DESC'	=> generate_text_for_display($row['newsletter_desc'], '', '', ''),
				
				'U_VIEW_NEWSLETTER'	=> append_sid("{$server_path}newsletter/index.$phpEx", 'view=detail&amp;news=' . $row['newsletter_id']),
			));
		break;
	}
}
$db->sql_freeresult($result);

$links = ($view != 'list') ? array(
	'mods/index.' . $phpEx	=> $user->lang['MODS'],
	"viewforum.$phpEx?f=39"	=> $user->lang['MODS_IN_DEVELOPMENT'],
	"viewforum.$phpEx?f=40" => $user->lang['REQUEST_A_MOD'],
) : array();

foreach ($links as $url => $lang)
{
	$template->assign_block_vars('navlinks', array(
		'U_VIEW_FORUM'	=> append_sid($server_path . $url),
		'FORUM_NAME'	=> $lang,
	));
}

$pagination = generate_pagination(append_sid("{$server_path}newsletter/index.$phpEx", "sd=$sort_dir"), $total_newsletters, $config['posts_per_page'], $start);

// global vars setup.
$template->assign_vars(array(
	'S_NEWSLETTER_LIST'	=> ($view == 'list') ? true : false,
	
	'PAGINATION'		=> $pagination,
	'PAGE_NUMBER'		=> on_page($total_newsletters, $config['posts_per_page'], $start),
	'TOTAL_NEWSLETTERS'	=> ($total_newsletters == 1) ? $user->lang['VIEW_NEWSLETTER'] : sprintf($user->lang['VIEW_NEWSLETTERS'], $total_newsletters),
	'S_BASE'			=> '<base href="' . generate_board_url() . '/newsletter/" />',
	'NEWSLETTER_VERSION'=> $newsletter_version,
	'U_LOGIN'			=> append_sid("{$server_path}ucp.$phpEx", 'mode=login'),
	'L_HELLO_USERNAME'	=> sprintf($user->lang['HELLO_USERNAME'], ($view == 'email') ? '[full name]' : get_username_string('full', $user->data['user_id'], $user->data['username'], $user->data['user_colour'], $user->lang['GUEST'])),
	
	'S_IS_EMAIL'		=> ($view == 'email' && $user->data['is_registered']) ? true : false,
	'U_UNSUBSCRIBE'		=> append_sid("{$server_path}ucp.$phpEx", 'i=prefs&amp;mode=personal'),
));

page_header($page_title);

$template->set_filenames(array(
	'body' => ($view == 'list') ? 'newsletter_list.html' : 'newsletter_body.html')
);

page_footer();
?>